<?php 

require("connect.php");

$read = mysql_query("SELECT * FROM location ORDER BY last_updated DESC") or die(mysql_error());

$locations = array();

$out = "<pre>";

while ($row = mysql_fetch_assoc($read)) {

	$out .= print_r($row, true);
	$out .= "\n";

	if (!array_key_exists($row['address'], $locations)) {
		$locations[$row[address]] = array('num_going' => 0, 'num_coming' => 0);
	}

	if ($row['type'] == "PICKUP") {
		$locations[$row[address]]['num_coming'] += $row['num_people'];
	} else {
		$locations[$row[address]]['num_going'] += $row['num_people'];
	}
}

$out .= "</pre>";

foreach ($locations as $address => $nums) {
	echo "<strong>$address</strong><br/>Pickup: $nums[num_coming]; Dropoff: $nums[num_going]<br/>";
}

echo "<hr/>$out";

?>